#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int dfn[N], low[N], deg[N], sz[N], scc[N], outdeg[N], indeg[N], dfncnt, scccnt;
bool instk[N];
vector<int> adj[N];
int w[N], a[N], d[N];
int W[N], V[N], dp[N][N];
stack<int> s;
int n, m;
void tarjan(int u) {
  dfn[u] = low[u] = ++dfncnt;
  s.push(u);
  instk[u] = true;
  for (auto v : adj[u]) {
    if (!dfn[v]) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (instk[v]) {
      low[u] = min(low[u], dfn[v]);
    }
  }
  if (dfn[u] == low[u]) {
    scccnt++;
    int k = -1;
    do {
      k = s.top();
      s.pop();
      sz[scccnt]++;
      instk[k] = false;
      scc[k] = scccnt;
      W[scccnt] += w[k];
      V[scccnt] += a[k];
    } while (k != u);
  }
}
void solve(int u) {
  for (int i = W[u]; i <= m; ++i) {
    dp[u][i] = V[u];
  }
  for (auto v : adj[u]) {
    solve(v);
    int k = m - W[u];
    for (int i = k; i >= 0; --i) {
      for (int j = 0; j <= i; ++j) {
        dp[u][i + W[u]] = max(dp[u][i + W[u]], dp[v][j] + dp[u][i + W[u] - j]);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> w[i];
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    cin >> d[i];
    if (d[i]) adj[d[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i)
    if (!dfn[i]) tarjan(i);
  for (int i = 0; i <= n; ++i) adj[i].clear();
  for (int i = 1; i <= n; ++i) {
    if (scc[d[i]] != scc[i]) {
      adj[scc[d[i]]].push_back(scc[i]);
      indeg[scc[i]]++;
    }
  }
  for (int i = 1; i <= scccnt; ++i) {
    if (!indeg[i]) adj[0].push_back(i);
  }

  solve(0);
  cout << dp[0][m] << endl;
}
